package advertising

import (
	"context"
	"device-admin/internal/app/grpc/client"
	"device-admin/internal/app/grpc/protoc/advertisingc"

	"192.168.1.75/go-pkg/errorx"
)

type PublishDevice struct{}

// 商户列表
func (PublishDevice) PublishMerchantList(ctx context.Context, params *advertisingc.PublishMerchantListReq) (
	data []*advertisingc.PublishMerchantListRsp_MerchantList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.PublishMerchantList(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return data, count, errorx.New(rsp.Msg, int(rsp.Code))
	}

	if rsp.List == nil {
		rsp.List = []*advertisingc.PublishMerchantListRsp_MerchantList{}
	}

	return rsp.List, rsp.Total, nil
}

// 商户配置投放设备组
func (PublishDevice) PublishMerchantManage(ctx context.Context, params *advertisingc.PublishMerchantManageReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.PublishMerchantManage(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return errorx.New(rsp.Msg, int(rsp.Code))
	}

	return
}

// 投放设备组设备列表
func (PublishDevice) PublishDeviceList(ctx context.Context, params *advertisingc.PublishDeviceListReq) (
	data []*advertisingc.PublishDeviceListRsp_Data_PublishDeviceList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.PublishDeviceList(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return data, count, errorx.New(rsp.Msg, int(rsp.Code))
	}

	if rsp.Data.List == nil {
		rsp.Data.List = []*advertisingc.PublishDeviceListRsp_Data_PublishDeviceList{}
	}
	return rsp.Data.List, rsp.Data.Count, nil
}

// 投放设备组列表
func (PublishDevice) PublishDeviceGroupList(ctx context.Context, params *advertisingc.PublishDeviceGroupListReq) (
	data []*advertisingc.PublishDeviceGroupListRsp_Data_DeviceGroupList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.PublishDeviceGroupList(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return data, count, errorx.New(rsp.Msg, int(rsp.Code))
	}

	if rsp.Data.List == nil {
		rsp.Data.List = []*advertisingc.PublishDeviceGroupListRsp_Data_DeviceGroupList{}
	}
	return rsp.Data.List, rsp.Data.Count, nil
}

// 投放设备组新增编辑
func (PublishDevice) PublishDeviceGroupSave(ctx context.Context, params *advertisingc.PublishDeviceGroupSaveReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.PublishDeviceGroupSave(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return errorx.New(rsp.Msg, int(rsp.Code))
	}

	return
}

// 投放设备组删除
func (PublishDevice) PublishDeviceGroupDelete(ctx context.Context, params *advertisingc.PublishDeviceGroupDeleteReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.PublishDeviceGroupDelete(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return errorx.New(rsp.Msg, int(rsp.Code))
	}

	return
}

// 投放设备组配置设备
func (PublishDevice) PublishDeviceGroupManage(ctx context.Context, params *advertisingc.PublishDeviceGroupManageReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.PublishDeviceGroupManage(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return errorx.New(rsp.Msg, int(rsp.Code))
	}

	return
}
